/**
 * Created by Acery on 2017/7/29.
 */

/*统一动画接口前缀*/
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame;

/*统一AudioContext接口前缀*/
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext;


/*canvas绘图函数*/
_drawSpectrum = function (analyser) {
	var that = this,
			canvas = document.getElementById('canvas'),
			cwidth = canvas.width,
			cheight = canvas.height - 2,
			meterWidth = 10, //width of the meters in the spectrum // 单位长度
			gap = 2, //gap between meters // 间隔
			capHeight = 2,
			capStyle = '#fff',
			meterNum = 800 / (10 + 2), //count of the meters // 能量槽数目
			capYPositionArray = []; ////store the vertical position of hte caps for the preivous frame
	ctx = canvas.getContext('2d'),
			gradient = ctx.createLinearGradient(0, 0, 0, 300); //线性渐变
	gradient.addColorStop(1, '#0f0');
	gradient.addColorStop(0.5, '#ff0');
	gradient.addColorStop(0, '#f00');
	var drawMeter = function () {
		var array = new Uint8Array(analyser.frequencyBinCount); // array长度和frequencyBinCount长度相等
		analyser.getByteFrequencyData(array); // 将当前的频率数据传入array
		// console.log(array);
		// console.log(analyser.frequencyBinCount);
		var step = Math.round(array.length / meterNum); //sample limited data from the total array
		ctx.clearRect(0, 0, cwidth, cheight);
		for (var i = 0; i < meterNum; i++) {
			var value = array[i * step];
			if (capYPositionArray.length < Math.round(meterNum)) {
				capYPositionArray.push(value);
			}
			;
			ctx.fillStyle = capStyle;
			//draw the cap, with transition effect
			if (value < capYPositionArray[i]) {
				ctx.fillRect(i * 12, cheight - (--capYPositionArray[i]), meterWidth, capHeight);
			} else {
				ctx.fillRect(i * 12, cheight - value, meterWidth, capHeight);
				capYPositionArray[i] = value;
			}
			;
			ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
			ctx.fillRect(i * 12 /*meterWidth+gap*/, cheight - value + capHeight, meterWidth, cheight); //the meter
		}
		requestAnimationFrame(drawMeter) // 递归调用
	}
	requestAnimationFrame(drawMeter);
}


readFileAndAnalysis = function (files) {
	let file = files[0]; // 拿到上传的文件
	console.log(file)
	let fr = new FileReader(); // 申请读取文件的对象
	let audioBox = new AudioContext(); // 申请一个音频容器，可以对数据进行解码
	
	fr.onload = function (e) {
		let fileResult = e.target.result; // buffer数据
		audioBox.decodeAudioData(fileResult, function (buffer) { // 对fileResult进行解码，解码后将buffer传入回调函数
			let audioSourceNode = audioBox.createBufferSource();//给盒子创建一个输入流
			let analyser = audioBox.createAnalyser() ; // 创建分析node
			audioSourceNode.buffer = buffer; //输入流引入 缓冲准备
			audioSourceNode.connect(analyser); // 链接上音频分析
			_drawSpectrum(analyser); // 绘画函数
			analyser.connect(audioBox.destination); // 连接上音频输出
			setTimeout(() => {
				audioSourceNode.start(0);
			}, 2000);
			audioSourceNode.start(0);// 播放 音频流进入audioBox
		}); // 音频盒子对buffer数据进行解码

	};
	fr.readAsArrayBuffer(file); // 读文件
	
}

let a 

if(!a==undefined){
	
}
